﻿using hedefgrup.reports.renault.database;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

public class user
{
    private string[] dealerCodes;
    private string firstName;
    private long id;
    private bool isActive;
    private bool isAdministrator;
    private bool isDeleted;
    private string lastName;
    private string newPassword;
    private string[] projects;
    private string userName;
    private string authLevel;

    private user(long id, string userName, string firstName, string lastName, bool isActive, bool isAdministrator, string[] projects, string[] dealerCodes, string authLevel)
    {
        this.id = id;
        this.userName = userName;
        this.firstName = firstName;
        this.lastName = lastName;
        this.isActive = isActive;
        this.isAdministrator = isAdministrator;
        this.projects = projects;
        this.dealerCodes = dealerCodes;
        this.authLevel = authLevel;
    }

    public static bool exists(string userName)
    {
        return (db.query("SELECT [id] FROM [user] (NOLOCK) WHERE [user_name] = @p_0", new object[] { userName }).Rows.Count > 0);
    }

    public static user[] getAllUsers()
    {
        List<user> users = new List<user>();
        DataTable dt = db.query("SELECT * FROM [user] ORDER BY [id] ASC", new object[0]);
        foreach (DataRow row in dt.Rows)
        {
            users.Add(getUserFromDataRow(row));
        }
        return users.ToArray();
    }

    public string[] getDealerCodes()
    {
        this.throwIfDeleted();
        return this.dealerCodes;
    }

    public string getFirstName()
    {
        this.throwIfDeleted();
        return this.firstName;
    }

    public long getID()
    {
        this.throwIfDeleted();
        return this.id;
    }

    public bool getIsActive()
    {
        this.throwIfDeleted();
        return this.isActive;
    }

    public bool getIsAdministrator()
    {
        this.throwIfDeleted();
        return this.isAdministrator;
    }

    public string getLastName()
    {
        this.throwIfDeleted();
        return this.lastName;
    }

    public string[] getProjects()
    {
        this.throwIfDeleted();
        return this.projects;
    }

    public string getAuthLevel()
    {
        this.throwIfDeleted();
        return this.authLevel;
    }

    public static user getUserByCredentials(string userName, string password)
    {
        return getUserFromDataTable(db.query("SELECT * FROM [user] WHERE [user_name] = @p_0 AND pwdCompare(@p_1, password) = 1", new object[] { userName, password }));
    }

    public static user getUserById(long id)
    {
        return getUserFromDataTable(db.query("SELECT * FROM [user] WHERE [id] = @p_0", new object[] { id }));
    }

    private static user getUserFromDataRow(DataRow userDataRow)
    {
        return new user(Convert.ToInt64(userDataRow["id"]), Convert.ToString(userDataRow["user_name"]).Trim(), Convert.ToString(userDataRow["first_name"]).Trim(), Convert.ToString(userDataRow["last_name"]).Trim(), Convert.ToBoolean(userDataRow["active"]), Convert.ToBoolean(userDataRow["is_administrative_user"]), readProjects(userDataRow), Convert.ToString(userDataRow["dealercode"]).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries), Convert.ToString(userDataRow["AuthLevel"]));
    }

    private static user getUserFromDataTable(DataTable userData)
    {
        if (userData.Rows.Count == 0)
        {
            return null;
        }
        return getUserFromDataRow(userData.Rows[0]);
    }

    public string getUserName()
    {
        this.throwIfDeleted();
        return this.userName;
    }

    public bool Insert()
    {
        this.throwIfDeleted();
        if (this.id > 0L)
        {
            return false;
        }
        this.id = db.execute("INSERT INTO [user] (user_name, password, first_name, last_name, active, is_administrative_user, project, dealercode, authLevel) VALUES (@p_0, pwdencrypt(@p_1), @p_2, @p_3, @p_4, 0, @p_5, @p_6, @p_7)\r\nSELECT @@IDENTITY", true, new object[] { this.userName, this.newPassword, this.firstName, this.lastName, this.isActive, string.Join(",", this.projects), string.Join(",", this.dealerCodes), this.authLevel });
        return true;
    }

    public static user New()
    {
        return new user(0L, "", "", "", false, false, new string[] { }, new string[] { }, "");
    }

    private static string[] readProjects(DataRow userDataRow)
    {
        string[] projects = Convert.ToString(userDataRow["project"]).Split(new char[] { ',' });
        for (int i = 0; i < projects.Length; i++)
        {
            projects[i] = projects[i].Trim();
        }
        return projects;
    }

    public void Remove()
    {
        this.throwIfDeleted();
        db.execute("DELETE [user] WHERE [id] = @p_0", new object[] { this.id });
        this.isDeleted = true;
    }

    public void setActivationStatus(bool isActive)
    {
        this.isActive = isActive;
    }

    public void setDealerCodes(string[] newValue)
    {
        this.dealerCodes = newValue;
    }

    public void setFirstName(string newValue)
    {
        this.firstName = newValue;
    }

    public void setLastName(string newValue)
    {
        this.lastName = newValue;
    }

    public void SetPassword(string newPassword)
    {
        this.throwIfDeleted();
        this.newPassword = newPassword;
    }

    public bool SetPassword(string oldPassword, string newPassword)
    {
        this.throwIfDeleted();
        if (this.id <= 0L)
        {
            return false;
        }
        DataTable dt = db.query("SELECT * FROM [user] WHERE [id] = @p_0 AND pwdCompare(@p_1, [password]) = 1", new object[] { this.id, oldPassword });
        if (dt.Rows.Count == 1)
        {
            this.newPassword = newPassword;
        }
        return (dt.Rows.Count == 1);
    }

    public bool SetProjects(string[] projects)
    {
        this.projects = projects;
        return true;
    }

    public bool setUserName(string userName)
    {
        if (exists(userName))
        {
            return false;
        }
        this.userName = userName;
        return true;
    }

    public void SetAuthLevel(string authLevel)
    {
        this.throwIfDeleted();
        this.authLevel = authLevel;
    }

    private void throwIfDeleted()
    {
        if (this.isDeleted)
        {
            throw new InvalidOperationException("No operation can be made on a deleted instance");
        }
    }

    public bool Update()
    {
        this.throwIfDeleted();
        if (this.id <= 0L)
        {
            return false;
        }
        bool isPasswordChanged = !string.IsNullOrEmpty(this.newPassword);
        object[] prms = new object[isPasswordChanged ? 9 : 8];
        prms[0] = this.firstName;
        prms[1] = this.lastName;
        prms[2] = this.isActive;
        prms[3] = this.id;
        prms[4] = this.userName;
        prms[5] = string.Join(",", this.projects);
        prms[6] = string.Join(",", this.dealerCodes);
        prms[7] = this.authLevel;

        if (isPasswordChanged)
        {
            prms[8] = this.newPassword;
        }
        db.execute("UPDATE [user] SET [user_name] = @p_4, project = @p_5, password = " + (isPasswordChanged ? "pwdencrypt(@p_8)" : "password") + ", first_name = @p_0, last_name = @p_1, active = @p_2, dealercode = @p_6, authLevel = @p_7 WHERE [id] = @p_3", prms);
        this.newPassword = null;
        return true;
    }

    public static user Current
    {
        get
        {
            return ((HttpContext.Current.Session["user_instance"] != null) ? (HttpContext.Current.Session["user_instance"] as user) : null);
        }
        set
        {
            HttpContext.Current.Session["user_instance"] = value;
        }
    }
}

